﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyUpdate.Utils;
using MyUpdate.Entity;
using System.IO;

namespace MyUpdate
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            loadServerAddress();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.Hide();
            new UpdateForm(tbServerAddress.Text,tbPort.Text).ShowDialog();
            this.Show();
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            this.Hide();
            new RecoverForm().ShowDialog();
            this.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            string srt = tbServerAddress.Text + "\n" + tbPort.Text;
            SaveServerAddress(srt);
            this.Close();
        }

        private void SaveServerAddress(string ApplicationData)
        {
            StreamWriter saveApplicationDataToTxt = new StreamWriter(Application.StartupPath + "\\UpdateServerAddress.cfg", false);
            try
            {
                saveApplicationDataToTxt.WriteLine(ApplicationData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                saveApplicationDataToTxt.Flush();
                saveApplicationDataToTxt.Close();
            }
        }

        private void loadServerAddress()
        {
            FileStream fs1 = new FileStream(Application.StartupPath + "\\UpdateServerAddress.cfg", FileMode.OpenOrCreate);
            StreamReader sw = new StreamReader(fs1);
            try
            {

                string ServerAddress = sw.ReadLine();
                string Port = sw.ReadLine();
                if (ServerAddress != null && Port != null)
                {
                    tbServerAddress.Text = ServerAddress;
                    tbPort.Text = Port;
                }
            }
            catch
            { }
            finally
            {
                sw.Close();
                fs1.Close();
            }
        }
    }
}
